package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.PhysicalExaminationDao;
import com.example.child_protection.dao.PhysicalExaminationValues2Dao;
import com.example.child_protection.dao.PhysicalExaminationValuesDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.PhysicalExaminationDTO;
import com.example.child_protection.dto.info.PhysicalExaminationValues2DTO;
import com.example.child_protection.dto.info.PhysicalExaminationValuesDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.PhysicalExaminationRequestDTO;
import com.example.child_protection.dto.response.PhysicalExaminationListResponseDTO;
import com.example.child_protection.dto.response.PhysicalExaminationResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.CategoryEnums;
import com.example.child_protection.enums.ResponseCodeEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.PhysicalExaminationModel;
import com.example.child_protection.model.PhysicalExaminationValues2Model;
import com.example.child_protection.model.PhysicalExaminationValuesModel;
import com.example.child_protection.service.PhysicalExaminationQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 儿童档案查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class PhysicalExaminationQueryServiceImpl extends MyBaseService implements PhysicalExaminationQueryService {

    @Autowired
    public PhysicalExaminationDao physicalExaminationDao;

    @Autowired
    public PhysicalExaminationValuesDao physicalExaminationValuesDao;

    @Autowired
    public PhysicalExaminationValues2Dao physicalExaminationValues2Dao;


    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public PhysicalExaminationResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_FIND_ID, requestDTO, PhysicalExaminationResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, PhysicalExaminationResponseDTO>() {
            @Override
            protected PhysicalExaminationResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                PhysicalExaminationResponseDTO responseDTO = new PhysicalExaminationResponseDTO();
                PhysicalExaminationModel physicalExaminationModel = physicalExaminationDao.findId(requestDTO.getId());
                PhysicalExaminationDTO physicalExamination = new PhysicalExaminationDTO();
                BeanUtils.copyProperties(physicalExaminationModel, physicalExamination);
                getDetails(physicalExamination);
                responseDTO.setInfo(physicalExamination);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public PhysicalExaminationListResponseDTO findList(PhysicalExaminationRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_FIND_LIST, requestDTO, PhysicalExaminationListResponseDTO.class, new MyServiceExecutor<PhysicalExaminationRequestDTO, PhysicalExaminationListResponseDTO>() {
            @Override
            protected PhysicalExaminationListResponseDTO myprocess(PhysicalExaminationRequestDTO requestDTO) throws Exception {
                PhysicalExaminationListResponseDTO responseDTO = new PhysicalExaminationListResponseDTO();
                PhysicalExaminationModel req = new PhysicalExaminationModel();
                BeanUtils.copyProperties(requestDTO, req);
                if ( null != requestDTO.getNums() ){
                    if (requestDTO.getNums() == 0 ){
                        throw  new Exception(ResponseCodeEnums.ILLEGAL_ARGUMENT_EXCEPTION.getDisplayName());
                    }
                    req.setPage(requestDTO.getNums() - 1);
                }
                List<PhysicalExaminationModel> lists = physicalExaminationDao.findPage(req);
                List<PhysicalExaminationDTO> pes = JSONObject.parseArray(JSONArray.toJSONString(lists), PhysicalExaminationDTO.class);

                if (requestDTO.getIsOnlyRoot().equals(BooleanEnums.F)) {
                    for (PhysicalExaminationDTO pe : pes) {
                        getDetails(pe);
                    }
                }

                responseDTO.setInfos(pes);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }


    /**
     * 统计体检总次数
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO count(PhysicalExaminationRequestDTO requestDTO){
        return this.service(ServiceActionEnums.DEPARTMENT_FIND_LIST, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<PhysicalExaminationRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(PhysicalExaminationRequestDTO requestDTO) throws Exception {
                BaseResponseDTO responseDTO = new BaseResponseDTO();
                PhysicalExaminationModel req = new PhysicalExaminationModel();
                BeanUtils.copyProperties(requestDTO, req);
                int res = physicalExaminationDao.count(req);

                responseDTO.setId(new Long(res+""));
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    private void getDetails(PhysicalExaminationDTO pe) {

        PhysicalExaminationValuesModel pev = new PhysicalExaminationValuesModel();
        PhysicalExaminationValues2Model pev2 = new PhysicalExaminationValues2Model();

        /**
         * 查询体检信息
         */
        pev.setPeId(pe.getId());
        List<PhysicalExaminationValuesModel> pevs = physicalExaminationValuesDao.findPage(pev);
        if (null != pevs)
            pe.setValues(JSONObject.parseArray(JSONArray.toJSONString(pevs), PhysicalExaminationValuesDTO.class));

        /**
         * 查询饮食调查
         */
        pev2.setPeId(pe.getId());
        pev2.setCategoryId(CategoryEnums.CHILD_FOOD_CHECK.getCode());
        List<PhysicalExaminationValues2Model> pevs2 = physicalExaminationValues2Dao.findPage(pev2);
        if (null != pevs2)
            pe.setValues2(JSONObject.parseArray(JSONArray.toJSONString(pevs2), PhysicalExaminationValues2DTO.class));

        /**
         * 查询体征信息
         */
        pev2.setCategoryId(CategoryEnums.CHILD_SIGN.getCode());
        List<PhysicalExaminationValues2Model> pevs3 = physicalExaminationValues2Dao.findPage(pev2);
        if (null != pevs3)
            pe.setValues3(JSONObject.parseArray(JSONArray.toJSONString(pevs3), PhysicalExaminationValues2DTO.class));

        /**
         * 查询spat信息
         */
        pev2.setCategoryId(CategoryEnums.CHILD_SPT_TEST.getCode());
        List<PhysicalExaminationValues2Model> pevs4 = physicalExaminationValues2Dao.findPage(pev2);
        if (null != pevs4)
            pe.setValues4(JSONObject.parseArray(JSONArray.toJSONString(pevs4), PhysicalExaminationValues2DTO.class));


        /**
         * 查询结果
         */
        pev2.setCategoryId(CategoryEnums.CHILD_RESULT.getCode());
        List<PhysicalExaminationValues2Model> pevs5 = physicalExaminationValues2Dao.findPage(pev2);
        if (null != pevs5)
            pe.setValues5(JSONObject.parseArray(JSONArray.toJSONString(pevs5), PhysicalExaminationValues2DTO.class));

    }
}
